<?php

namespace Tasker\Authorization;

class Acl extends \Nette\Object
{

	/**
	 * @var \NotORM
	 */
	private $connection;

	public function __construct(\NotORM $connection)
	{
		$this->connection = $connection;
	}

	public function getGlobalRoles()
	{
		return $this->connection->user_role();
	}

	public function getGlobalResources()
	{
		return $this->connection->user_permission()->select('distinct resource');
	}

	public function getGlobalRules()
	{
		return $this->connection->user_permission_role();
	}

	public function getProjectRoles()
	{
		return $this->connection->project_role();
	}

	public function getProjectResources()
	{
		return $this->connection->project_permission()->select('distinct resource');
	}

	public function getProjectRules()
	{
		return $this->connection->project_permission_role();
	}

	public function hasRoleInProject($userId, $projectId, $role)
	{
		$r = $this->connection->project_member()->where('user_id = ? AND project_id = ? AND project_role.role = ?', $userId, $projectId, $role);
		return (count($r) > 0);
	}

}
